<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaNoticeController extends OaBaseController
{
    var $model;
    var $cur_emp_id;
    var $appid;
    var $type = ['公告','会议','任务督办'];

    function _initialize()
    {
        $this->model = $this->getModel('oa_notice');
        parent::_initialize();
        $this->curUserInfo = $this->getCurUserInfo();
        $this->cur_emp_id = $this->curUserInfo['emp_id'];
        $this->appid = get_app_info()['appid'];

//        $controller = strtolower(CONTROLLER_NAME);
//        $action = strtolower(ACTION_NAME);
//
//        $res ['title'] = '公告通知督办信息';
//        $res ['url'] = addons_url('OaSystem://OaNotice/announcement_list', array('mdm' => I('mdm')));
//        $res ['class'] = $controller == 'OaNotice' ? 'current' : '';
//        $nav [] = $res;
//        $this->assign('nav', $nav);

//        $res ['title'] = '普通通知栏';
//        $res ['class'] = $action == 'announcement_list' ? 'cur' : '';
//        $sub_nav[] = $res;
//        $res ['title'] = '会议通知栏';
//        $res ['url'] = addons_url('OaSystem://OaNotice/notice_list/', array('mdm' => I('mdm')));
//        $res ['class'] = $action == 'notice_list' ? 'cur' : '';
//        $sub_nav[] = $res;
//        $res ['title'] = '任务督办栏';
//        $res ['url'] = addons_url('OaSystem://OaNotice/suppervise_list/', array('mdm' => I('mdm')));
//        $res ['class'] = $action == 'suppervise_list' ? 'cur' : '';
//        $sub_nav[] = $res;
//        $this->assign('sub_nav', $sub_nav);
    }

    public function jump() {
        if(!empty($this->first_sub_nav_url))
            redirect($this->first_sub_nav_url);
    }

    public function announcement_list () {
        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;
        $map['type'] = 0;
        $map['creater'] = $this->cur_emp_id;
        $len = D('OaNotice')->where($map)->count();
        $announcements = M('OaNotice')->where($map)->order('time desc')->page($p,$row)->select();
        for ($i = 0; $i < count($announcements); $i++) {
            $announcements[$i]['time'] = date('Y-m-d H:i:s',$announcements[$i]['time']);
            $toUsers = json_decode($announcements[$i]['to_users'],true);
            $user_map['id'] = array('in',$toUsers);
            $receivers = M('OaEmployee')->where($user_map)->field('name')->select();
            $announcements[$i]['to_users'] = implode(";", array_column($receivers, 'name'));
        }
        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }

        $this->assign('editAction','announcement_edit');
        $this->assign('detailAction','announcement_detail');
        $this->assign('addUrl',addons_url('OaSystem://OaNotice/announcement_add'));
        $this->assign('type','普通通知');
        $this->assign('_page',$list_data['_page']);
        $this->assign('announcements',$announcements);
        $this->display();
    }

    private function get_employee_in_dept ($deptId) {
        $map['dept_id'] = $deptId;
        $employees = M('OaEmployee')->where($map)->field('id,name')->select();
        // var_dump('get_employee_in_dept:'.$deptId);
        $sub_depts = M('OaDept')->where(array('pid'=>$deptId))->select();
        foreach ($sub_depts as $sub_dept) {
            $tmp = $this->get_employee_in_dept($sub_dept['id']);
            $employees = array_merge($employees,$tmp);
        }
        // var_dump($employees);
        return $employees;
    }

    private function update_announcement ($type,$id = null) {
        $tmpEmps = json_decode($_POST['to_users'],true);
        $toUsers = array();
        // var_dump($tmpEmps['depts']);
        foreach ($tmpEmps['depts'] as $dept) {
            $tmps = $this->get_employee_in_dept($dept);
            foreach ($tmps as $tmp) {
                $toUsers[] = $tmp['id'];
            }
        }
        if (!empty($_POST['leader'])) {
            $data['leader'] = $_POST['leader'];
        }
        $toUsers = array_merge($toUsers,$tmpEmps['employees']);
        $toUsers = array_unique($toUsers);
        $data['to_users'] = json_encode($toUsers);
        $data['appid'] = $this->appid;
        $data['time'] = NOW_TIME;
        $data['type'] = $type;  // 0: 公告
        $data['creater'] = intval($this->cur_emp_id);
        $data['title'] = $_POST['title'];
        if ($type === 1) {
            $data['title'] = $data['title'].';'.$_POST['meetTime'].';'.$_POST['meetRoom'];
        }
        $data['details'] = $_POST['details'];
        $data['from_who'] = $_POST['from_who'];


        $files = json_decode($_POST['fileids'],true);
        for ($i = 0; $i < count($files); $i++){
            $tmp = M('File')->where(array('id'=>$files[$i]['id']))->find();
            $files[$i]['url'] = "/Uploads/Download/".$tmp['savepath'].$tmp['savename'];
        }
        $data['fileids'] = json_encode($files);
        $is_sendWxMsg = ($_POST['sendMsg'] == 'on') ? true : false;
        $sendMsg = false;
        if (empty($id)){
            $ret = M('OaNotice')->add($data);
            $data['id'] = $ret;
        } else {
            $data['id'] = $id;
            $map['id'] = $data['id'];
            $oldNotice =  M('OaNotice')->where($map)->find();
            $ret = M('OaNotice')->where($map)->save($data);
            $old_toUsers = json_decode($oldNotice['to_users']);
            $new_toUsers = array();
            foreach ($toUsers as $toUser) {
                if (!in_array($toUser,$old_toUsers )){
                    $new_toUsers[] = $toUser;
                }
            }
            if (count($new_toUsers) > 0) {
                $toUsers = $new_toUsers;
                $sendMsg = true;
            }
        }

        if ($ret === false) {
            return $ret;
        }else if((empty($id)) || ($sendMsg === true)){
            $new_data = M('OaNotice')->where(array('id'=>$data['id']))->find();
            $content['title'] = $new_data['title'];
            $content['time'] = date("Y/m/d h:i:s",$new_data['time']);
            $content['details'] = $new_data['details'];
            $content['from'] = $new_data['from_who'];
            if(!in_array($this->cur_emp_id,$toUsers)){
                array_push($toUsers,$this->cur_emp_id);
            }
            if((!in_array($data['leader'],$toUsers)) and ($type == 2)){
                array_push($toUsers,$data['leader']);
            }
            foreach ($toUsers as $toUser) {
                switch ($type){
                    case 0:  // 普通通知
                        D('OaEmpMsg')->sendAnnoucementMsg($toUser,$new_data['time'],$content,$new_data['id'],$is_sendWxMsg);
                        break;
                    case 1:  // 会议通知
                        D('OaEmpMsg')->sendNoticeMsg($toUser,$new_data['time'],$content,$new_data['id'],$is_sendWxMsg);
                        break;
                    case 2: // 任务督办
                        D('OaNoticeTaskReply')->sendSupperviseMsg($toUser,$this->cur_emp_id,$new_data,$is_sendWxMsg);
                        D('OaEmpMsg')->sendSupperviseMsg($toUser,$new_data,$is_sendWxMsg);
                        break;
                }
            }
        }
        return $data['id'];
    }
    public function announcement_add () {
        if(IS_POST) {
            $ret = $this->update_announcement(0);
            if ($ret === false) {
                $this->error ( '创建普通通知'. '失败！');
            }else{
                $url = addons_url('OaSystem://OaNotice/announcement_list', array('mdm' => I('mdm')));
                $this->success ( '发送普通通知' . '成功！',$url);
            }

        } else {
            $get_depts_url = addons_url('OaSystem://OaCommonJsApi/getDeptsSelectList');
            $get_employee_url = addons_url('OaSystem://OaCommonJsApi/getEmployeeInDept');
            $this->assign('type','普通通知');
            $this->assign('get_depts',$get_depts_url);
            $this->assign('get_employee',$get_employee_url);
            $this->assign('post_url',addons_url('OaSystem://OaNotice/announcement_add'));
            $this->display();
        }
    }

    public function announcement_edit() {

        if(IS_POST) {
            $id = $_POST['id'];
            $ret = $this->update_announcement(0,$id);
            if ($ret === false) {
                $this->error ( '修改普通通知'. '失败！');
            }else{
                $url = addons_url('OaSystem://OaNotice/announcement_list', array('mdm' => I('mdm')));
                $this->success ( '发送普通通知' . '成功！',$url);
            }
        } else {
            $id = I('id');
            $data = M('OaNotice')->where(array('id'=>$id))->find();
            $users['employees']= json_decode($data['to_users'],true);
            $users['depts'] = [];
            $data['to_users'] = json_encode($users);

            $tmpReceivers = [];
            foreach ($users['employees'] as $emp) {
                $tmpReceivers[] = M('OaEmployee')->where(array('id'=>$emp))->getField('name');
            }
            $data['receivers'] = implode(";", $tmpReceivers);

            $get_depts_url = addons_url('OaSystem://OaCommonJsApi/getDeptsSelectList');
            $get_employee_url = addons_url('OaSystem://OaCommonJsApi/getEmployeeInDept');

            $this->assign('type','普通通知');
            $this->assign('data',$data);
            $this->assign('get_depts',$get_depts_url);
            $this->assign('get_employee',$get_employee_url);
            $this->assign('post_url',addons_url('OaSystem://OaNotice/announcement_edit/id/'.$id));
            $this->display('announcement_add');
        }
    }

    public function announcement_detail() {
        $map['id'] = I('id');
        $data = M('OaNotice')->where($map)->find();

        $toUsers = json_decode($data['to_users'],true);
        $user_map['id'] = array('in',$toUsers);
        $receivers = M('OaEmployee')->where($user_map)->field('name')->select();
        $data['to_users'] = implode(";", array_column($receivers, 'name'));

        $files = json_decode($data['fileids'],true);
        for($i = 0; $i < count($files); $i++){
            $files[$i]['url'] = SITE_URL.$files[$i]['url'];
        }

        $this->assign('data',$data);
        $this->assign('files',$files);
        $this->display();
    }

    public function notice_list () {
        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;
        $map['type'] = 1;  // 通知
        $map['creater'] = $this->cur_emp_id;
        $len = D('OaNotice')->where($map)->count();
        $announcements = M('OaNotice')->where($map)->order('time desc')->page($p,$row)->select();
        for ($i = 0; $i < count($announcements); $i++) {
            $announcements[$i]['time'] = date('Y-m-d H:i:s',$announcements[$i]['time']);
            $toUsers = json_decode($announcements[$i]['to_users'],true);
            $user_map['id'] = array('in',$toUsers);
            $receivers = M('OaEmployee')->where($user_map)->field('name')->select();
            $announcements[$i]['to_users'] = implode(";", array_column($receivers, 'name'));
        }
        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }

        $this->assign('editAction','notice_edit');
        $this->assign('detailAction','notice_detail');
        $this->assign('addUrl',addons_url('OaSystem://OaNotice/notice_add'));
        $this->assign('type','会议通知');
        $this->assign('_page',$list_data['_page']);
        $this->assign('announcements',$announcements);
        $this->display('announcement_list');
    }
    public function notice_add() {
        if(IS_POST) {
            $ret = $this->update_announcement(1);
            if ($ret === false) {
                $this->error ( '创建会议通知'. '失败！');
            }else{
                $url = addons_url('OaSystem://OaNotice/notice_list', array('mdm' => I('mdm')));
                $this->success ( '发送会议通知' . '成功！',$url);
            }

        } else {
            $get_depts_url = addons_url('OaSystem://OaCommonJsApi/getDeptsSelectList');
            $get_employee_url = addons_url('OaSystem://OaCommonJsApi/getEmployeeInDept');
            $this->assign('type','通知');
            $this->assign('get_depts',$get_depts_url);
            $this->assign('get_employee',$get_employee_url);
            $this->assign('post_url',addons_url('OaSystem://OaNotice/notice_add'));
            $this->display();
        }
    }

    public function notice_edit() {
        if(IS_POST) {
            $id = $_POST['id'];
            $ret = $this->update_announcement(1,$id);
            if ($ret === false) {
                $this->error ( '修改会议通知'. '失败！');
            }else{
                $url = addons_url('OaSystem://OaNotice/notice_list', array('mdm' => I('mdm')));
                $this->success ( '发送会议通知' . '成功！',$url);
            }
        } else {
            $id = I('id');
            $data = M('OaNotice')->where(array('id'=>$id))->find();
            $users['employees']= json_decode($data['to_users'],true);
            $users['depts'] = [];
            $data['to_users'] = json_encode($users);

            $tmpReceivers = [];
            foreach ($users['employees'] as $emp) {
                $tmpReceivers[] = M('OaEmployee')->where(array('id'=>$emp))->getField('name');
            }
            $data['receivers'] = implode(";", $tmpReceivers);

            $title = explode(';',$data['title']);
            $data['title'] = $title[0];
            $data['meetTime'] = $title[1];
            $data['meetRoom'] = $title[2];

            $get_depts_url = addons_url('OaSystem://OaCommonJsApi/getDeptsSelectList');
            $get_employee_url = addons_url('OaSystem://OaCommonJsApi/getEmployeeInDept');

            $this->assign('type','会议通知');
            $this->assign('data',$data);
            $this->assign('get_depts',$get_depts_url);
            $this->assign('get_employee',$get_employee_url);
            $this->assign('post_url',addons_url('OaSystem://OaNotice/notice_edit/id/'.$id));
            $this->display('notice_add');
        }
    }
    public function notice_detail() {
        $map['id'] = I('id');
        $data = M('OaNotice')->where($map)->find();

        $toUsers = json_decode($data['to_users'],true);
        $user_map['id'] = array('in',$toUsers);
        $receivers = M('OaEmployee')->where($user_map)->field('name')->select();
        $data['to_users'] = implode(";", array_column($receivers, 'name'));
        $title = explode(' ',$data['title']);
        $data['title'] = $title[0];
        $data['meetTime'] = $title[1];
        $data['meetRoom'] = $title[2];

        $files = json_decode($data['fileids'],true);
        for ($i = 0; $i < count($files); $i++){
            $tmp = M('File')->where(array('id'=>$files[$i]['id']))->find();
            $files[$i]['url'] = SITE_URL."/Uploads/Download/".$tmp['savepath'].$tmp['savename'];
        }

        $this->assign('data',$data);
        $this->assign('files',$files);
        $this->display();
    }

    public function show_receivers() {
        $map['noticeid'] = I('noticeid');

        $notices = D('OaEmpMsgNoticeView')->where($map)->select();

        $data = array();
        foreach ($notices as $notice) {
            $tmp['name'] = $notice['receivername'];
            if(empty($notice['reply'])){
                if(empty($notice['is_read'])){
                    $tmp['status'] = '未读';
                    $tmp['color'] = 'background: #00FF99';
                }else{
                    $tmp['status'] = '已读';
                    $tmp['color'] = 'background: #2a85a0';
                }
            }else{
                if ($notice['reply'] == 1){
                    $tmp['status'] = '确认参加';
                    $tmp['color'] = 'background: #3eb94e';
                } else if ($notice['reply'] == 2) {
                    $tmp['status'] = '拒绝参加';
                    $tmp['color'] = 'background: #3eb94e';
                }

            }
            array_push($data,$tmp);
        }

        $this->assign('data',$data);
        $this->display();
    }

    public function suppervise_list() {
        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;
        $map['type'] = 2;  // 任务督办
        $map['creater'] = $this->cur_emp_id;
        $len = D('OaNotice')->where($map)->count();
        $announcements = M('OaNotice')->where($map)->order('time desc')->page($p,$row)->select();
        for ($i = 0; $i < count($announcements); $i++) {
            $announcements[$i]['time'] = date('Y-m-d H:i:s',$announcements[$i]['time']);
            $toUsers = json_decode($announcements[$i]['to_users'],true);
            $user_map['id'] = array('in',$toUsers);
            $receivers = M('OaEmployee')->where($user_map)->field('name')->select();
            $announcements[$i]['to_users'] = implode(";", array_column($receivers, 'name'));
            $taskStateStr = ['已发送', '已发送', '新消息', '已处理', '已完成'];
            $status = empty($announcements[$i]['status'])?0:$announcements[$i]['status'];
            $announcements[$i]['status'] = $taskStateStr[$status];
        }
        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }

        $this->assign('editAction','suppervise_edit');
        $this->assign('detailAction','suppervise_detail');
        $this->assign('addUrl',addons_url('OaSystem://OaNotice/suppervise_add'));
        $this->assign('type','任务督办');
        $this->assign('_page',$list_data['_page']);
        $this->assign('announcements',$announcements);
        $this->display();
    }

    public function suppervise_add() {
        if(IS_POST) {
            if(empty($this->cur_emp_id)){
                $this->error ( '创建任务督办'. '失败！(管理员不能创建任务督办）');
                return;
            }
            if(empty($_POST['leader'])){
                $this->error ( '请选择督办领导');
                return;
            }

            $ret = $this->update_announcement(2);
            if ($ret === false) {
                $this->error ( '创建任务督办'. '失败！');
            }else{
                $url = addons_url('OaSystem://OaNotice/suppervise_list', array('mdm' => I('mdm')));
                $this->success ( '发送任务督办' . '成功！',$url);
            }
        } else {
            $map['role_id'] = 14; // 14 is 督办领导 角色
            $usersRole = D('OaUserRoleEmployeeView')->where($map)->select();
            $leaders = array();
            $leader['emp_id'] = $userRole['0'];
            $leader['emp_name'] = '选择督办领导';
            $leader['selected'] = '';
            $leaders[] = $leader;
            foreach ($usersRole as $userRole) {
                $leader['emp_id'] = $userRole['emp_id'];
                $leader['emp_name'] = $userRole['emp_name'];
                $leader['selected'] = '';
                $leaders[] = $leader;
            }

            $get_depts_url = addons_url('OaSystem://OaCommonJsApi/getDeptsSelectList');
            $get_employee_url = addons_url('OaSystem://OaCommonJsApi/getEmployeeInDept');

            $this->assign('leaders',$leaders);
            $this->assign('type','任务督办');
            $this->assign('get_depts',$get_depts_url);
            $this->assign('get_employee',$get_employee_url);
            $this->assign('post_url',addons_url('OaSystem://OaNotice/suppervise_add'));
            $this->display();
        }
    }
    public function suppervise_edit() {
        if(IS_POST) {
            $id = $_POST['id'];
            $ret = $this->update_announcement(2,$id);
            if ($ret === false) {
                $this->error ( '修改任务督办'. '失败！');
            }else{
                $data = M('OaNotice')->where(array('id'=>$id))->find();
                $map['noticeid'] = $id;
                $map['pid'] = 0;
                $father_replies = M('OaNoticeTaskReply')->where($map)->select();
                foreach ($father_replies as $father_reply) {
                    $father_reply['comment'] = $data['details'];
                    $father_reply['time'] = $data['time'];
                    M('OaNoticeTaskReply')->where(array('id'=>$father_replies['id']))->save($father_reply);
                }
                $url = addons_url('OaSystem://OaNotice/suppervise_list', array('mdm' => I('mdm')));
                $this->success ( '发送任务督办' . '成功！',$url);
            }
        } else {
            $id = I('id');
            $data = M('OaNotice')->where(array('id'=>$id))->find();
            $users['employees']= json_decode($data['to_users'],true);
            $users['depts'] = [];
            $data['to_users'] = json_encode($users);
            $tmpReceivers = [];
            foreach ($users['employees'] as $emp) {
                $tmpReceivers[] = M('OaEmployee')->where(array('id'=>$emp))->getField('name');
            }
            $data['receivers'] = implode(";", $tmpReceivers);

            $ur_map['role_id'] = 14; // 14 is 督办领导 角色
            $usersRole = D('OaUserRoleEmployeeView')->where($ur_map)->select();
            $leaders = array();
            $leader['emp_id'] = $userRole['0'];
            $leader['emp_name'] = '选择督办领导';
            $leader['selected'] = '';
            $leaders[] = $leader;
            foreach ($usersRole as $userRole) {
                $leader['emp_id'] = $userRole['emp_id'];
                $leader['emp_name'] = $userRole['emp_name'];
                $leader['selected'] = ($data['leader'] == $userRole['emp_id'])?'selected':'';
                $leaders[] = $leader;
            }

            $get_depts_url = addons_url('OaSystem://OaCommonJsApi/getDeptsSelectList');
            $get_employee_url = addons_url('OaSystem://OaCommonJsApi/getEmployeeInDept');

            $this->assign('leaders',$leaders);
            $this->assign('type','任务督办');
            $this->assign('data',$data);
            $this->assign('get_depts',$get_depts_url);
            $this->assign('get_employee',$get_employee_url);
            $this->assign('post_url',addons_url('OaSystem://OaNotice/suppervise_edit/id/'.$id));
            $this->display('suppervise_add');
        }
    }

    public function suppervise_detail() {
        $map['id'] = I('id');
        $data = M('OaNotice')->where($map)->find();

        $toUsers = json_decode($data['to_users'],true);
        $user_map['id'] = array('in',$toUsers);
        $receivers = M('OaEmployee')->where($user_map)->field('id,name')->select();
        $data['to_users'] = implode(";", array_column($receivers, 'name'));
        $data['time'] = date("Y-m-d H:i:s",$data['time']);
        $files = json_decode($data['fileids'],true);
        for($i = 0; $i < count($files); $i++){
            $files[$i]['url'] = SITE_URL.$files[$i]['url'];
        }

        $StatusStr = ['未读','已读','未处理','已读未处理','已处理','已完成'];
        $usersReply = array();
        $ntr_map['noticeid'] = I('id');
        foreach ($receivers as $receiver) {
            $replies = array();
            $ntr_map['receiver'] = $receiver['id'];
            $ntr_map['pid'] = 0;
            $fatherReply = D('OaNoticeTaskReplySenderView')->where($ntr_map)->find();
            // array_push($replies,$tmp);
            $ntr_map['pid'] = $fatherReply['id'];
            unset($ntr_map['receiver']);
            $tmplist = D('OaNoticeTaskReplySenderView')->where($ntr_map)->order('time')->select();
            $replies = array_merge($replies,$tmplist);
            $userReply['name'] = $receiver['name']."(".$StatusStr[$fatherReply['is_read']].")";
            $hasNew = false;
            for($i = 0; $i < count($replies); $i++){
                if(($replies[$i]['is_read'] == 0) && ($replies[$i]['receiver'] == $this->cur_emp_id)){
                    $hasNew = true;
                }
                if(empty($replies[$i]['fileids'])){
                    continue;
                }
                $replies[$i]['fileids'] = json_decode($replies[$i]['fileids'],true);
            }
            if($hasNew === true) {
                $userReply['name'] = $receiver['name'].'( *新）';
            }
            $userReply['replies'] = $replies;
            $userReply['noticeid'] = I('id');
            $userReply['pid'] = $fatherReply['id'];
            array_push($usersReply,$userReply);
        }

        $this->assign('data',$data);
        $this->assign('files',$files);
        $this->assign('usersReply',$usersReply);
        $this->display();
    }

    public function addNewSupperviseReply () {
        if(IS_POST) {
            $data['comment'] = $_POST['comment'];
            $data['pid'] = $_POST['pid'];
            $data['noticeid'] = $_POST['noticeId'];
            $data['time'] = NOW_TIME;
            $data['sender'] = $this->cur_emp_id;
            $data['receiver'] = $_POST['receiver'];
            $data['is_read'] = 0;
            $files = json_decode($_POST['fileids'],true);
            for ($i = 0; $i < count($files); $i++){
                $tmp = M('File')->where(array('id'=>$files[$i]['id']))->find();
                $files[$i]['url'] = "/Uploads/Download/".$tmp['savepath'].$tmp['savename'];
            }
            $data['fileids'] = json_encode($files);
            $ret = M('OaNoticeTaskReply')->add($data);
            if ($ret === false) {
                $this->error ( '回复消息'. '失败！');
            }else{
                $father_reply = M('OaNoticeTaskReply')->where(array('id'=>$_POST['pid']))->find();
                $father_reply['is_read'] = 2;  // 更新为未处理状态
                M('OaNoticeTaskReply')->save($father_reply);
                $notice = M('OaNotice')->where(array('id'=>$data['noticeid']))->find();
                $notice['status'] = 3;
                M('OaNotice')->save($notice);
                $url = addons_url('OaSystem://OaNotice/suppervise_detail/id/'.$data['noticeid'], array('mdm' => I('mdm')));
                $this->success ( '回复消息' . '成功！',$url);
            }
        } else {
            if((empty(I('noticeId'))) || (empty(I('pid')))){
                $this->error ( '参数错误！！！');
            }
            $notice = M('OaNotice')->where(array('id'=>I('noticeId')))->find();
            $father_reply = M('OaNoticeTaskReply')->where(array('id'=>I('pid')))->find();
            $this->assign('pid',I('pid'));
            $this->assign('noticeId',I('noticeId'));
            $this->assign('title',$notice['title']);
            $this->assign('receiver',$father_reply['receiver']);
            $this->assign('post_url',addons_url('OaSystem://OaNotice/addNewSupperviseReply/noticeId/'.$notice['id'].'/pid/'.I('pid')));
            $this->display('add_reply');
        }
    }

    public function setTaskDone () {
        if((empty(I('noticeId'))) || (empty(I('pid')))){
            $this->error ( '参数错误！！！');
        }
        $notice = M('OaNotice')->where(array('id'=>I('noticeId')))->find();
        $father_reply = M('OaNoticeTaskReply')->where(array('id'=>I('pid')))->find();
        $father_reply['is_read'] = 5;  // 任务完成
        M('OaNoticeTaskReply')->save($father_reply);
        $allFathers = M('OaNoticeTaskReply')->where(array('noticeid'=>I('noticeId'),'pid'=>0))->select();
        $alltaskHasDone = true;
        foreach ($allFathers as $father) {
            if ($father['is_read'] != 5) {
                $alltaskHasDone = false;
                break;
            }
        }
        if ($alltaskHasDone === true) {
            $notice['status'] = 4;
            M('OaNotice')->save($notice);
        }
        $url = addons_url('OaSystem://OaNotice/suppervise_detail/id/'.I('noticeId'), array('mdm' => I('mdm')));
        $this->success ( '状态更新' . '成功！',$url);
    }
}